#!/usr/bin/php
<?php

// This is an exported function of the basic maths code generation
// logic from mr_lunch. This is not used, except as testing against
// new implementations.

function generateMTCode($id, $objectName)
{
        $iniLength = 5;
        // remove any punctuation
        $cleanName = preg_replace("/[^A-Za-z0-9\s]/","",$objectName);
        // remove mulitple spaces
        $cleanName = preg_replace('/\s\s+/', ' ', $cleanName);
        // count the number of words in the name
        $numWords = str_word_count($cleanName);
        // explode at the remaining spaces
        $words = explode(" ", $cleanName);
        $shortWords = array('OF', 'TO', 'IN', 'IT', 'IS', 'BE', 'AS', 'AT', 'SO',
                        'BY', 'OR', 'ON', 'DO', 'IF', 'AN', 'THE', 'AND', 'DAS');

        foreach ($words as $wrd) {
          if ($numWords < ($iniLength +1)) {
            if(in_array(strtoupper($wrd), $shortWords)) {
              $howMany = 1;
            }else{
              $howMany = ($iniLength+1-$numWords);
            }
          }else{
            $howMany = 1;
          }
          $objectInitials .= strtoupper(substr($wrd, 0, $howMany));
        }
        // pad the id with zeroes, add a dash and the initials on the end
        $mtCode = $id.'-'.str_pad(substr($objectInitials, 0, $iniLength), $iniLength, "X", STR_PAD_RIGHT);
        return $mtCode;
}


$name = implode(" ", array_slice($argv, 1));
print $name . "\n";
print generateMTCode("0000000", $name) . "\n";


?>